<?php 
namespace Admin\Model;
use Think\Model;
class CustomerModel extends Model {
    
    public function AllCustomerList($field_arr=false,$lr,$meid) {
    	$time = time();
    	$customer_mdb = M('user');
        $channel_db = D('Channel');
    	$orderoperation = D('order_operation');
        if($field_arr){
            $field_str = implode(',',$field_arr);
        }else{
            $field_str = true;
        }
        
        $medata = M('admin')->where('id='.$meid)->field('id,position,provinceid,cityid')->find();
        if($medata['position']==1){
            $dataor = implode(',',array_column(M('admin')->join('jdc_order_operation on jdc_order_operation.usernum=jdc_admin.usernum')->where('jdc_admin.id='.$meid.' and jdc_order_operation.status=0')->field('jdc_order_operation.orderid')->select(),'orderid'));
            $w_h = 'orderid in('.$dataor.')';
        }else if($medata['position']==3){
            M('admin')->where('id='.$meid)->field('id,position,provinceid,cityid')->find();
            $adminids = M('admin')->where('pid='.$medata['id'])->field('id')->select();
            if(!$adminids){
                return [];
            }

            $adminids = implode(',',array_column($adminids, 'id'));
            $ww_ = '1=1 and  id  in('.$adminids.')';
            $adm = M('admin')->where($ww_)->select();
            for($i = 0; $i < count($adm); $i++){
                  if($s){
                      $s.=",'".$adm[$i]['usernum']."'";
                  }else{
                      $s.="'".$adm[$i]['usernum']."'";
                  }
            }
            $usernums = '1=1 and  usernum  in('.$s.')';
            $operation = $orderoperation->where($usernums)->select();
            $orderids = implode(',',array_column($operation, 'orderid'));
            $ww_ = ' and pay.orderid in('.$orderids.')';
            // $w_h = 'jdc_order.cityid='.$medata['cityid'];
        }else if($medata['position']==4){
            $w_h = 'jdc_order.provinceid='.$medata['provinceid'];
        }
        $orderids = M('order')->where($w_h)->field('orderid')->select();
        if($orderids){
            $orderids = implode(',',array_column($orderids,'orderid'));
            $where = ' and pay.orderid in('.$orderids.')';
            $customer_data = $customer_mdb
            ->join('jdc_order_month_pay pay ON pay.uid = jdc_user.id')
            ->where(' pay.status = 0 and  FROM_UNIXTIME(pay.pay_time,"%Y%m%d")<'.date('Ymd',$time).$where.$ww_)
            ->limit($lr[0],$lr[1])
            ->group('orderid')
            ->field($field_str)
            ->select();
        }else{
            $customer_data = [];
        }
        $arr_AB = [1=>'A',2=>'B'];
        foreach ($customer_data as $key => $value) {
        	$value['type'] = $arr_AB[$channel_db->OnefieldData(['waynum'=>$value['waynum']],['type'])['type']];
        	$value['wayid'] = $channel_db->OnefieldData(['waynum'=>$value['waynum']],['id'])['id'];
        	$value['yuqi'] = $this->OneWhereCount($value['id'],'yuqi');
        	$customer_data[$key] = $value;
        }
        return $customer_data;
    }

    /**
     * @param. $id. userid
     * $type.  
     */
    public function OneWhereCount($id,$type) {
    	$time = time();
    	$orderPay_mdb = M('order_month_pay');
    	$user_mdb = M('user');
    	if($type=='yuqi'){//用户逾期单总数
    		$user_data = $user_mdb->where('id='.$id)->find();
		    if(!$user_data){
		        return 0;
		    }
		    $timeoutcount = [];
		    $orderPay_one_data = $orderPay_mdb->where('uid='.$id)->group('orderid')->select();
		    if(!$orderPay_one_data){
		        return 0;
		    }
		    foreach ($orderPay_one_data as $k => $v) {
		        if(date('Ymd',$v['pay_time'])<date('Ymd',$time)){
		            $timeoutcount[$v['uid']] += 1;
		        }
		    }
		    return array_sum($timeoutcount);
    	}
    }

    /**
     * 获取单个数据
     */
    public function OnefieldData($id,$field=false) {
    	$customer_mdb = M('user');
    	if($field){
            $field = implode(',', $field);
        }else{
            $field = true;
        }
        if($id){
            $id_key = array_keys($id);
            $id_value = array_values($id);
            $where = $id_key[0].'='.$id_value[0];
        }else{
            return false;
        }
        $customer_data = $customer_mdb->where($where)->field($field)->find();
        return $customer_data;
    }

    /**
     * 获取用户数据
     * 
     */
    public function UserInfo($id) {
        $user_mdb = M('user');
        $user_data = $user_mdb -> where('id='.$id)->find();
        $isjh = ['1'=>'已婚有子女','2'=>'已婚无子女','3'=>'未婚','4'=>'其他'];
        $user_data['age'] = date('Y')-date('Y',$user_data['birthday']);
        $shouru = [1=>'10001以上',2=>'8001-10000',3=>'5001-8000',4=>'4001-5000',5=>'3001-4000',6=>'2001-3000',7=>'1001-2000',8=>'1000以下'];
        $user_data['marriage'] = $isjh[$user_data['marriage']];
        $creditcard = ['1'=>'三张以下','2'=>'三张以上'];
        $user_data['creditcard'] = $creditcard[$user_data['creditcard']];
        $borrowing = ['1'=>'无借款','2'=>'有借款但已还清','3'=>'有借款未还清'];
        $user_data['borrowing'] = $borrowing[$user_data['borrowing']];
        return $user_data;
    }
    
    /**
     * 添加用户数据
     */
    public function UserInsert($data) {
        $user_mdb = M('user');
        $InsertData = [
            'nickname'=>$data['phone'],
            'tel'=>$data['phone'],
            'waynum'=>$data['waynum'],
            'create_time'=>$data['time'],
        ];
        $result = $user_mdb->add($InsertData);
        if($result){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 修改用户数据
     */
    public function Update($where,$data) {
        $user_mdb = M('user');
        $result = $user_mdb->where($where)->save($data);
        if($result){
            return true;
        }else{
            return false;
        }
    }
}